//
//  ZHTimeView.m
//  Doctor
//
//  Created by liuzhishan on 15/12/16.
//  Copyright © 2015年 shanshan. All rights reserved.
//

#import "ZHTimeView.h"
#import "DataTool.h"
#import "ZHAlertView.h"
#import <objc/runtime.h>

#define LINECOLOR [UIColor colorWithRed:100/255.0 green:200/255.0 blue:250/255.0 alpha:1]
#define BTNCOLOR [UIColor colorWithRed:102/255.0 green:229/255.0 blue:233/255.0 alpha:1]

@interface ZHTimeView ()

@property (nonatomic,strong) ZHAlertView *alertView;

@end

@implementation ZHTimeView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self addMyChildViews];
    
    
    }
    return self;
}


-(void)dateChangeAction:(UIDatePicker*)date
{
    
    
    
    NSString *str = nil;
    
    if (date == self.firstPicker) {
        
        str = [DataTool getStrWithDate:date.date];
        self.firstTime = str;
        
    }
    
    else
    {
        
          str = [DataTool getStrWithDate:date.date];
          self.lastTime = str;
        
    }
    
    
//    if([DataTool firstDate:self.firstPicker.date andSecondDate:self.lastPicker.date andCompareInterval:60])
//    {
//        self.firstPicker.date = [DataTool getDate:self.lastPicker.date Before:30];
//        //nslog(@"777777%@",[DataTool getStrWithDate:self.firstPicker.date]);
//        
//        self.firstTime = [DataTool getStrWithDate:self.firstPicker.date];
//        
//    }

    
}

#pragma mark 添加子视图
-(void)addMyChildViews
{
    self.firstPicker = [[UIDatePicker alloc]init];
     self.firstPicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"Chinese"];
     self.firstPicker.datePickerMode = UIDatePickerModeDate;
   
    [ self.firstPicker setMyFramewithX:10 andY:0 andWidth:240 andHeight:150];
    [self addSubview: self.firstPicker];
    [ self.firstPicker addTarget:self action:@selector(dateChangeAction:) forControlEvents:UIControlEventValueChanged];
    
    NSDate *dateStr = [DataTool getDateBefore:31];
 
    [ self.firstPicker setDate:dateStr animated:YES];
    self.firstTime = [DataTool getStrWithDate:self.firstPicker.date];
    
    

    
    
     self.lastPicker = [[UIDatePicker alloc]init];
     self.lastPicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"Chinese"];
     self.lastPicker.datePickerMode = UIDatePickerModeDate;
    [ self.lastPicker setMyFramewithX:300 andY:0 andWidth:240 andHeight:150];
    [self addSubview: self.lastPicker];
    [ self.lastPicker addTarget:self action:@selector(dateChangeAction:) forControlEvents:UIControlEventValueChanged];
    
    dateStr = [DataTool getDateBefore:1];
    self.lastPicker.maximumDate = dateStr;
    self.firstPicker.maximumDate = dateStr;
    [ self.lastPicker setDate:dateStr animated:YES];
    self.lastTime = [DataTool getStrWithDate: self.lastPicker.date];
    
    
    
//    self.firstPicker.minimumDate = [DataTool getDate:self.lastPicker.date Before:60];
//    self.firstPicker.maximumDate =  self.lastPicker.date;

    
        [self changePickerColor];
        [self removePickerLine:self.firstPicker];
        [self removePickerLine:self.lastPicker];
    
    
    
    
     UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
     [btn setMyFramewithX:580 andY:55 andWidth:100 andHeight:40];
     btn.backgroundColor = BTNCOLOR;
     [btn setBackgroundImage:[UIImage imageNamed:@"Query button"] forState:(UIControlStateNormal)];
     btn.titleLabel.font = [UIFont systemFontOfSize:30 * WIDPro -2];
     [btn addTarget:self action:@selector(btnInquireAction) forControlEvents:(UIControlEventTouchUpInside)];
     [self addSubview:btn];
    

    
}

-(void)removePickerLine:(UIDatePicker * )picker
{
    UIView *viewDownLine = picker.subviews[0].subviews[2];
    UIView *viewUpLine = picker.subviews[0].subviews[1];
    viewDownLine.hidden = YES;
    viewUpLine.hidden = YES;
    
}

-(void)changePickerColor
{
    
    SEL selector = NSSelectorFromString(@"setHighlightsToday:");
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDatePicker instanceMethodSignatureForSelector:selector]];
    BOOL no = NO;
    [invocation setSelector:selector];
    [invocation setArgument:&no atIndex:2];
    [invocation invokeWithTarget:self.firstPicker];
    [invocation invokeWithTarget:self.lastPicker];

    unsigned outCount;
    int i;
    objc_property_t *pProperty = class_copyPropertyList([UIDatePicker class], &outCount);
    for (i = outCount -1; i >= 0; i--)
    {
        // 循环获取属性的名字   property_getName函数返回一个属性的名称
        
        NSString *getPropertyName = [NSString stringWithCString:property_getName(pProperty[i]) encoding:NSUTF8StringEncoding];
//        NSString *getPropertyNameString = [NSString stringWithCString:property_getAttributes(pProperty[i]) encoding:NSUTF8StringEncoding];
        
        //nslog(@"%@",getPropertyName);
        
        if([getPropertyName isEqualToString:@"textColor"])
        {
            
            [self.firstPicker setValue:[UIColor whiteColor] forKey:@"textColor"];
            [self.lastPicker setValue:[UIColor whiteColor] forKey:@"textColor"];
        }
        
        
    }
    
    
}


-(void)btnInquireAction
{
    
    
    if([DataTool firstDate:self.firstPicker.date andSecondDate:self.lastPicker.date andCompareInterval:60])
    {
        if (self.alertView == nil) {
            
            self.alertView = [[ZHAlertView alloc]init];
            self.alertView.layer.masksToBounds = YES;
            self.alertView.layer.cornerRadius = 10*WIDPro;
            [self.alertView setMyFramewithX:0 andY:0 andWidth:self.width/2.5 andHeight:self.height/2];
            self.alertView.centerX = self.width/2.5;
            self.alertView.centerY = self.height/1.5;
            
            [self.alertView setAlertMessage:@"日期差要小于60天且不能为负数喔 亲～" andUIFond:18 andAlpha:0.5];
            [self addSubview:self.alertView];
        }
        
        self.alertView.alpha = 1;
        
        [UIView animateWithDuration:2.0 animations:^{
            
            self.alertView.alpha = 0;
        }];
        
        return;
    }
    
    NSString *judgeStr =  [[NSUserDefaults standardUserDefaults] objectForKey:@"judgeAll"];
    
    NSString *clinicURL = @"";
    
    NSInteger index = [[[NSUserDefaults standardUserDefaults] objectForKey:@"VCIndex"] integerValue];
    
    if ([judgeStr isEqualToString:@"no"]) {
        
        NSNumber *nub = [[NSUserDefaults standardUserDefaults] objectForKey:@"ClinicID"];
        
        
        
        if (index == 1) {
            //srartTime=2015-11-10&finishTime=2016-1-6
            
              clinicURL = [NSString stringWithFormat:@"%@%@/%@?startTime=%@&finishTime=%@",CLINICDEFAULTSTATISTICSURL,[ZHGetOwerID getOwerID],nub,self.firstTime,self.lastTime];
            
        }
        
        else if (index == 2)
        {
            
             clinicURL = [NSString stringWithFormat:@"%@%@/%@?startTime=%@&finishTime=%@",CLINICDEFAULTINCOMEURL,[ZHGetOwerID getOwerID],nub,self.firstTime,self.lastTime];
            
        }
        
        else
        {
            
            clinicURL = [NSString stringWithFormat:@"%@%@/%@?startTime=%@&finishTime=%@",CLINICDEFAULTPATIENTERURL,[ZHGetOwerID getOwerID],nub,self.firstTime,self.lastTime];
            
        }
      
        
    }
    
    else
    {
        
        if (index == 1) {
            
            clinicURL = [NSString stringWithFormat:@"%@%@/%@?startTime=%@&finishTime=%@",CLINICDEFAULTSTATISTICSURL,[ZHGetOwerID getOwerID],@"all",self.firstTime,self.lastTime];
            
        }
        
        else if (index == 2)
        {
            
            clinicURL = [NSString stringWithFormat:@"%@%@/%@?startTime=%@&finishTime=%@",CLINICDEFAULTINCOMEURL,[ZHGetOwerID getOwerID],@"all",self.firstTime,self.lastTime];
            
        }
        
        else
        {
            
            clinicURL = [NSString stringWithFormat:@"%@%@/%@?startTime=%@&finishTime=%@",CLINICDEFAULTPATIENTERURL,[ZHGetOwerID getOwerID],@"all",self.firstTime,self.lastTime];
        }

        
    }

    NSLog(@"%@",clinicURL);
    
    
    
    [AFNData getAFNDataWithUrl:clinicURL success:^(id responseObject) {
        
         self.block(responseObject);
        
        
    } failure:^(NSError *error) {
        
        
    }];
    
}


-(void)setMyBlock:(ClickBlock)clock
{
    self.block = clock;
}

@end
